package com.caifu.util;

import com.caifu.controller.AbstractController;
import com.caifu.controller.ImageController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * @author tianwenwen
 * ClassName: ImageController.java
 * version: 1.0.0
 * createTime: 2020年08月28日 14:24:08
 **/
@RequestMapping("upload")
@Controller
public class ImageTaMaDeController extends AbstractController {

    private static final Logger logger = LoggerFactory.getLogger(ImageController.class);

    //测试
    @Value("${Image.IMAGE_UPLOAD_PATH}")
    private String fileUploadPath;

    @Value("${Image.IMAGE_RESULT_URL_PATH}")
    private String imageUrlPath;

    /*@Value("${file.upload.path}")
    private String fileUploadPath;

    @Value("${file.upload.result.path}")
    private String imageUrlPath;*/

    @RequestMapping("uploadImage")
    @ResponseBody
    public Map<String, Object> uploadImage(MultipartFile file) {
        Map<String, Object> map = new HashMap<>(15);
        try {
            String imageUrl = ImageUpload.imageSingleUpload(file, fileUploadPath, imageUrlPath);
            map.put("imageUrl", imageUrl);
            map.put("code", "200");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", "500");
            map.put("msg", "网络异常");
            logger.error(e.toString());
        }
        return map;
    }
}
